classdef OBJRegionLoader
%OBJLOADER Summary of this class goes here
%   Detailed explanation goes here

   properties
       File='';
       Vertices;
       Regions;
   end

   methods
       function this = OBJRegionLoader(filename)
           this.File = filename;
           input = fopen(this.File);
                      
           reg=0;
           v = 1;
           
           line = fgetl(input);
           while ischar(line)
               if isempty(line)
                   line = fgetl(input);
                   continue
               end
               ch = line(1);               
               
               if findstr(line, '# object') == 1 & length(line)>2
                   name = sscanf(line,'# object %s to come ...');
                   reg = reg+1;
                   this.Regions{reg} = Region;
                   this.Regions{reg}.Name = name;
               end
               
               if ch == '#'
                   line = fgetl(input);
                   continue
               elseif ch == 'v'
                   pts = sscanf(line,'v %f %f %f');
                   this.Vertices(v,1) = pts(1);
                   this.Vertices(v,2) = pts(2);
                   this.Vertices(v,3) = pts(3);
                   v = v+1;
               elseif ch == 'f'
                   pts = sscanf(line,'f %f %f %f');
                   this.Regions{reg}= ...
                       this.Regions{reg}.AddFace(pts(1), pts(2), pts(3));
               end
               line = fgetl(input);
           end
           fclose(input)
       end
       
       function Plot(this)
           for j=1:length(this.Regions)
               color = [rand(), rand(), rand()];
               for i=1:length(this.Regions{j}.Faces)
                  [a,b,c] = this.Regions{j}.GetFace(i);

                  hold on
                  fill( [this.Vertices(a,1)   ...
                         this.Vertices(b,1)   ...
                         this.Vertices(c,1)   ...
                         this.Vertices(a,1)], ...
                        [this.Vertices(a,2)   ...
                         this.Vertices(b,2)   ...
                         this.Vertices(c,2)   ...
                         this.Vertices(a,2)], ...
                         color);
                  line( [this.Vertices(a,1)   ...
                         this.Vertices(b,1)   ...
                         this.Vertices(c,1)   ...
                         this.Vertices(a,1)], ...
                        [this.Vertices(a,2)   ...
                         this.Vertices(b,2)   ...
                         this.Vertices(c,2)   ...
                         this.Vertices(a,2)], ...
                         'color', color);
               end
           end
       end
       
       function region = GetRegion(this, x,y)
           region = '';
           for j=1:length(this.Regions)
               found = 0;
               verts = this.Regions{j}.GetVertexList();
               X = [ this.Vertices(verts,1)', x]';
               Y = [ this.Vertices(verts,2)', y]';
               hull = convhull(X,Y);
               
               for i=1:length(hull)
                   if hull(i) == length(X);
                       found = 1;
                       break
                   end
               end
               if found == 0
                   region = this.Regions{j}.Name;
                   return
               end
           end
       end
       
       function region = GetRegion2(this, x,y)
           region = 0;
           for j=1:length(this.Regions)
               found = 0;
               verts = this.Regions{j}.GetVertexList();
               X = [ this.Vertices(verts,1)', x]';
               Y = [ this.Vertices(verts,2)', y]';
               hull = convhull(X,Y);
               
               for i=1:length(hull)
                   if hull(i) == length(X);
                       found = 1;
                       break
                   end
               end
               if found == 0
                   region =j;
                   return
               end
           end
       end
       
       function r = IsInRegion(this,region, x,y)
           r = 1;
           verts =this.Regions{region}.GetVertexList();
           X = [this.Vertices(verts,1)' , x]';
           Y = [this.Vertices(verts,2)' , y]';
           
           hull = convhull(X,Y);
           
           for i=1:length(hull)
               if hull(i) == length(X);
                   r = 0;
                   break;
               end
           end
       end
       
       
       
       function list = AnalyzePath(this, x, y)
           current = '';
           previous = '';
           list = {};
           counter = 1;
           for i=1:length(x)
               current = this.GetRegion(x(i), y(i));
               if (  ~strcmp(current,'')  && ...
                     ~strcmp(current, previous)     )
                   list{counter} = current;
                   counter = counter+1;
                   previous = current;
               end
           end
       end
       
       function [list, times] = AnalyzePath2(this,x,y)
           current = '';
           previous = '';
           list = {};
           times = [];
           counter = 1;
           time = 0;
           for i=1:length(x)
               current = this.GetRegion(x(i), y(i));
               time = time+1;
               if (  ~strcmp(current,'')  && ...
                     ~strcmp(current, previous)     )
                   list{counter} = current;
                   times(counter) = time;
                   counter = counter+1;
                   previous = current;
                   
                   time = 0;
               end
           end
       end
       
       
       function time = GetTimeInRegion(this, region,x,y)
           reg = 1; 
           for j=1:length(this.Regions)
               if  strcmp(this.Regions{j}.Name, region)
                   reg = j;
                   break
               end
           end
           time = 0;
           for j=1:length(x)
               if this.IsInRegion(reg, x(j), y(j))
                   time = time+1;
               end
           end
       end
   end
end 
